<!--
  Copyright 2016-2018 the original author or authors.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- clearfix is needed for smaller screens where gt-config-return-link is floated  -->
<div class="clearfix">
  <a class="gt-config-return-link"
     href="admin/user-list">
    Return to list
  </a>
</div>
<div class="card">
  <div class="card-header">
    <h2 ng-class="{'gt-lighten-font': !loaded && !httpError}">
      Users
      <span class="d-inline-block gt-separator">
        |
      </span>
      <span style="font-size: 24px;">{{heading}}</span>
    </h2>
  </div>
  <div class="card-body">
    <div ng-include src="'template/gt-loading-overlay.html'"></div>
    <div ng-include src="'template/gt-http-error-overlay.html'"></div>
    <div ng-form
         gt-form-autofocus-on-first-input
         name="formCtrl"
         style="padding-top: 15px;">
      <div gt-form-group
           gt-label="Username"
           gt-model="config.username"
           gt-disabled="!layout.adminEdit || username"
           gt-width="40em"
           gt-required="loaded">
      </div>
      <div gt-form-group
           gt-type="checkbox"
           gt-label="LDAP"
           gt-checkbox-label="Use LDAP for password"
           gt-model="config.ldap"
           gt-disabled="!layout.adminEdit"
           ng-if="ldapAvailable && config.username.toLowerCase() !== 'anonymous'">
      </div>
      <div gt-form-group
           gt-label="Password"
           gt-type="password"
           gt-model="page.password"
           gt-disabled="!layout.adminEdit"
           gt-width="20em"
           gt-required="loaded && (!username || originalConfig.ldap && !config.ldap)"
           ng-if="!config.ldap && config.username.toLowerCase() !== 'anonymous'">
      </div>
      <div gt-form-group
           gt-label="Verify password"
           gt-type="password"
           gt-model="page.verifyPassword"
           gt-disabled="!layout.adminEdit"
           gt-width="20em"
           gt-required="loaded && (!username || originalConfig.ldap && !config.ldap)"
           ng-if="!config.ldap && config.username.toLowerCase() !== 'anonymous'">
      </div>
      <fieldset class="form-group">
        <div class="row">
          <legend class="col-xl-3 gt-check-legend-xl">
            Roles
          </legend>
          <div class="col-xl-9">
            <div gt-spinner
                 gt-show="rolesLoading && !httpError"
                 class="gt-inline-spinner d-none"
                 style="height: 50px;">
            </div>
            <label class="custom-control custom-checkbox"
                   ng-repeat="role in allRoles | orderBy:'name'">
              <input type="checkbox"
                     class="custom-control-input"
                     ng-model="role.checked"
                     ng-disabled="!layout.adminEdit">
              <div class="custom-control-label">
                <span ng-if="role.available">
                  <a href="admin/role?name={{role.name}}">{{role.name}}</a>
                </span>
                <span ng-if="!role.available">
                  {{role.name}} <span class="gt-red" style="font-weight: 600; padding-left: 10px;">Role not found</span>
                </span>
              </div>
            </label>
          </div>
        </div>
      </fieldset>
      <div class="form-group row"
           ng-if="layout.adminEdit">
        <div class="offset-xl-3 col-xl-9">
          <div gt-button-group>
            <div gt-button
                 gt-label="{{config.version ? 'Save changes' : 'Add'}}"
                 gt-click="save(deferred)"
                 gt-validate-form="formCtrl"
                 class="d-inline-block"
                 id="gtSaveChanges">
            </div>
            <button ng-click="displayDeleteConfirmationModal()"
                    ng-if="config.version"
                    class="btn btn-danger gt-button-spacing">
              Delete
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="modal"
     role="dialog"
     tabindex="-1"
     id="deleteConfirmationModal">
  <div class="modal-dialog modal-dialog-centered"
       role="document"
       style="max-width: 500px;">
    <div class="modal-content">
      <div class="modal-header">
        <h3 class="modal-title">Delete user</h3>
        <button type="button"
                class="close"
                data-dismiss="modal"
                aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Are you sure you want to delete this user?</p>
        <div style="min-height: 60px;">
          <div class="d-inline-block">
            <div gt-spinner
                 gt-show="deleting"
                 class="gt-left-aligned-spinner"
                 style="width: 50px; margin-bottom: -30px;">
            </div>
          </div>
          <div class="gt-button-message gt-button-message-error"
               style="display: block;"
               ng-if="errorCannotDeleteLastUser">
            Cannot delete last user
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-danger" ng-click="delete()">Yes</button>
        <button class="btn btn-primary" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>
<div class="modal"
     role="dialog"
     tabindex="-1"
     id="saveWithNoRolesConfirmationModal">
  <div class="modal-dialog modal-dialog-centered"
       role="document"
       style="max-width: 500px;">
    <div class="modal-content">
      <div class="modal-header">
        <h3 class="modal-title">User has no role assignments</h3>
        <button type="button"
                class="close"
                data-dismiss="modal"
                aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Are you sure you want to save this user with no roles?</p>
        <p>Without any role assignments, this user will have no permissions.</p>
      </div>
      <div class="modal-footer">
        <button class="btn btn-secondary" ng-click="saveWithNoRoles()">Yes</button>
        <button class="btn btn-primary" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>
<!-- each page with confirmation dialog needs its own confirmation dom so that it is deleted on $destroy -->
<div ng-include="'template/gt-confirmation.html'"></div>
